package com.sjzd.entity;

import com.sjzd.ConfigParser;
import com.sjzd.menu.GameMenu;
import com.sjzd.message.BulletStatusMessage;
import com.sjzd.message.StatusMessageBase;
import com.sjzd.message.TankStatusMessage;
import com.sjzd.net.ClientNet;

import java.awt.*;

public class Bullet extends AbstractBase implements Publisher{
    // id
    private String id;
    // 所属坦克id用以记录杀敌数
    private String tankId;

    private BulletStatusMessage bulletStatusMessage;
    private ClientNet clientNet;

    public String getId() {
        return id;
    }

    public String getTankId() {
        return tankId;
    }

    public Bullet(GameMenu gameMenu, String id, int X, int Y, double sinX, double sinY, int speed) {
        this(gameMenu, id, null, X, Y, sinX, sinY, speed);
    }

    public Bullet(GameMenu gameMenu, String id, String tankId, int X, int Y, double sinX, double sinY, int speed) {
        super(gameMenu, X, Y, sinX, sinY, speed);
        this.id = id;
        this.tankId = tankId;
        this.clientNet = gameMenu.getClientNet();
    }

    public BulletStatusMessage getBulletStatusMessage() {
        return bulletStatusMessage;
    }

    public void setBulletStatusMessage(BulletStatusMessage bulletStatusMessage) {
        this.bulletStatusMessage = bulletStatusMessage;
    }

    @Override
    public void draw(AbstractBase abstractBase, ConfigParser configParser, Graphics g) {
        move();
        if (checkHit() || checkOverBoundary()) {
            isExist = false;
            gameMenu.getBulletList().remove(this);
            // 发送炮弹死亡信息, 如果是发生碰撞此处可以添加爆炸效果
            bulletStatusMessage.update(X, Y, isExist);
            publishMessage(bulletStatusMessage);
            return;
        }
        g.setColor(Color.GRAY);
        g.fillOval(X, Y, configParser.getBULLET_WIDTH(), configParser.getBULLET_HEIGHT());
    }

    @Override
    public void move() {
        X += configParser.getBULLET_SPEED() * sinX;
        Y += configParser.getBULLET_SPEED() * sinY;
    }


    /**
     * 综合碰撞检测，包括：
     *  与坦克的撞击；
     *  与墙的撞击；
     * @return
     */
    private boolean checkHit() {
        if (checkHitTank() || checkHitWall()) {
            return true;
        }
        return false;
    }

    private boolean checkHitTank() {
        for (Tank tank : gameMenu.getOtherTankList()) {
            if (getRectangle().intersects(tank.getRectangle())) {
                // TODO 发送给其他用户坦克毁灭信息
                tank.isExist = false;
                TankStatusMessage tankStatusMessage = tank.getTankStatusMessage();
                tankStatusMessage.update(tank.X, tank.Y, tank.sinX, tank.sinY, tank.isExist);
                tank.publishMessage(tankStatusMessage);
                return true;
            }
        }
        return false;
    }

    private boolean checkHitWall() {
        return false;
    }

    /**
     *  获取轮廓
     */
    @Override
    public Rectangle getRectangle() {
        rectangle.setBounds(X, Y, configParser.getBULLET_WIDTH(), configParser.getBULLET_HEIGHT());
        return rectangle;
    }

    /**
     *  越界检验
     * @return
     */
    private boolean checkOverBoundary() {
        if (X < 0 || Y < 0 ||
                X + configParser.getBULLET_WIDTH() > configParser.getWINDOW_WIDTH() ||
                Y + configParser.getBULLET_HEIGHT() > configParser.getWINDOW_HEIGHT()) {
            return true;
        }
        return false;
    }

    @Override
    public void publishMessage(StatusMessageBase statusMessageBase) {
        clientNet.sendMsg(statusMessageBase);
    }
}
